﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Paw.Services.Models.TablePermissions>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Table Permissions</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Table Permissions</h2>
    <p>
        Select which users can access each table.
    </p>
    <table>
        <tr>
            <th>
                Table
            </th>
            <th>
                Users
            </th>
            <th>
                Allow access to all users
            </th>
        </tr>
        <% 
            var users = ViewData["users"] as IList<string>;

            foreach (var item in Model)
            { %>
        <tr>
            <td>
                <%: item.Table %>
            </td>
            <td>
                <% foreach (var user in users)
                   {
                       var hasAccess = item.Users.Contains(user);
                       var addTablePermissionnActionUrl = this.Url.Action("AddTablePermission", new { table = "_table_", user = "_user_" });
                       var removeTablePermissionActionUrl = this.Url.Action("RemoveTablePermission", new { table = "_table_", user = "_user_" });
                %>
                <p>
                    <input id="<%= item.Table %>_<%= user %>" name="<%= item.Table %>_<%= user %>" onclick="SetPermission('<%= item.Table %>', '<%= user %>', '<%= addTablePermissionnActionUrl %>', '<%= removeTablePermissionActionUrl %>')"
                        type="checkbox" <%= hasAccess ? "checked=\"yes\"" : string.Empty %> <%= item.IsPublic ? "disabled=\"disabled\"" : string.Empty %> />
                    <label for="<%= item.Table %>_<%= user %>">
                        <%: user %></label>
                </p>
                <% } %>
            </td>
            <td align="center">
                <% var publicActionUrl = this.Url.Action("SetTablePublic", new { table = "_table_", isPublic = "_isPublic_" }); %>
                <input id="Public_<%= item.Table %>" onclick="SetPublic('<%= item.Table %>', '<%= publicActionUrl %>')"
                    type="checkbox" <%= item.IsPublic ? "checked=\"yes\"" : string.Empty %> />
            </td>
        </tr>
        <% } %>
    </table>
</asp:Content>
